#include"ThreadPool.h"

typedef struct trainFileContent_s
{
    int length;
    char data[1024];
} trainFileContent_t;


int transFile(int netfd)
{
	trainFileContent_t train; // 在栈上申请一个小火车
	char filename[] = "file.txt";
	train.length = strlen(filename); //先填火车头
	memcpy(train.data, filename, train.length); //填充火车车厢
	send(netfd, &train, sizeof(train.length) + train.length,
	     MSG_NOSIGNAL); //火车的实际长度 = 火车头长度+火车车厢长度

	int fd = open(filename, O_RDWR);
	struct stat statbuf;
	fstat(fd, &statbuf);
	off_t filesize = statbuf.st_size;
	train.length = sizeof(filesize);
	memcpy(train.data, &filesize, train.length);
	send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

	while (1) {
		ssize_t sret = read(fd, train.data, sizeof(train.data));
		train.length = sret;
		if (train.length != 1024 && train.length != -1) {
			printf("train.length = %d\n", train.length);
		}
		send(netfd, &train, sizeof(train.length) + train.length,
		     MSG_NOSIGNAL);
		// 当文件没有剩余内容时，read返回0，先装一个空的火车头，发出去以后，再退
		if (sret == 0) {
			break;
		}
	}
	return 0;
}
